/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-27
 */
package org.jeecg.modules.flow.callback.outbound.order;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.entity.vo.OutBoundReceiptSaveVO;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.flow.callback.outbound.util.CommonOutboundUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 横剪叫料条料 600出库单保存 回调bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-27
 */
@RequiredArgsConstructor
@Component
public class OutBoundReceiptHJJLTL600SaveBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsOutReceiptService wmsOutReceiptService;

    private final WorkFlowService workFlowService;
    /**
     * 横剪叫料条料 600出库单保存
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {

        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 获取请求参数
        baseFlowDto.translateClass(OutBoundReceiptSaveVO.class);
        OutBoundReceiptSaveVO saveVO = (OutBoundReceiptSaveVO) baseFlowDto.getData();

        WmsOutReceipt wmsOutReceipt = new WmsOutReceipt();
        BeanUtils.copyProperties(saveVO, wmsOutReceipt);
        String receiptId;
        String taskId = wmsOutReceipt.getWmsOutReceiptTaskId();
        if (StrUtil.isEmpty(wmsOutReceipt.getId())) {
            receiptId = wmsOutReceiptService.saveMain(wmsOutReceipt, saveVO.getWmsOutReceiptMaterialList(), false);
        } else {
            receiptId = wmsOutReceipt.getId();
            WmsOutReceipt wmsOutReceiptEntity = wmsOutReceiptService.getById(receiptId);
            if (wmsOutReceiptEntity == null) {
                throw new JeecgBootException("未找到对应数据");
            }
            wmsOutReceiptService.updateMain(wmsOutReceipt, saveVO.getWmsOutReceiptMaterialList(),false);
        }
        JSONObject jsonData=new JSONObject();
        jsonData.put("taskId",taskId);
        jsonData.put("receiptId",receiptId);
        jsonData.put("fromReceiptOrderFlag",true);
        // 回调任务的节点
        workFlowService.backAutoCommit(taskId,jsonData);
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(receiptId);
    }
}
